package com.yuanfeng.userms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.dto.userms.PlusEquityDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.activity.RedTemplateEntityVO;
import com.yuanfeng.userms.entity.UserPlusEquityEntity;
import com.yuanfeng.userms.feign.ActivityServiceClient;
import com.yuanfeng.userms.mapper.UserPlusEquityMapper;
import com.yuanfeng.userms.mapper.UserPlusInfoMapper;
import com.yuanfeng.userms.service.UserPlusEquityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *
 */
@Service
public class UserPlusEquityServiceImpl extends ServiceImpl<UserPlusEquityMapper, UserPlusEquityEntity>
    implements UserPlusEquityService{

    @Autowired
    private UserPlusInfoMapper userPlusInfoMapper;
    @Autowired
    private ActivityServiceClient activityServiceClient;

    @Override
    public ResponseResult selectPlusEquitys(List<Integer> id) {
        List<UserPlusEquityEntity> list=this.baseMapper.selectList(new LambdaQueryWrapper<UserPlusEquityEntity>()
                                        .eq(UserPlusEquityEntity::getEquityStatus,1)
                                        .in(UserPlusEquityEntity::getEquityId,id));
        return ResponseResult.success(list);
    }

    @Override
    public ResponseResult insertPlusEquity(PlusEquityDTO dto) {
        String userInfoStr = RedisUtil.get(YFConfig.YF_PLATFORM_TOKEN_USER + dto.getToken());
        if (null == userInfoStr || userInfoStr.equals("")) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }
        Integer integer = this.baseMapper.insertPlusEquity(dto);
        if (integer > 0){
            return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
        }
        return ResponseResult.fail(BizCodeEnume.OPERATION_FAILED);
    }

    @Override
    public ResponseResult deletePlusEquity(PlusEquityDTO dto) {
        String userInfoStr = RedisUtil.get(YFConfig.YF_PLATFORM_TOKEN_USER + dto.getToken());
        if (null == userInfoStr || userInfoStr.equals("")) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }
        ArrayList<Object> list = new ArrayList<>();
        List<String> selectthalitypes = this.baseMapper.selectthalitypes();
        for (String selectthalitype : selectthalitypes) {
            String[] split = selectthalitype.split(",");
            List<Integer> appIdList = Stream.of(split).map(Integer::parseInt).collect(Collectors.toList());
            list.addAll(appIdList);
        }
        boolean contains = list.contains(dto.getEquityId());
        if (contains){
            return ResponseResult.fail(BizCodeEnume.PLUS_ERROR);
        }
        //将信息备份
        UserPlusEquityEntity plusEquityEntity =this.baseMapper.selectById(dto.getEquityId());
        Integer integer1 =this.baseMapper.insertplusequityTemp(plusEquityEntity);

        Integer integer = this.baseMapper.deleteById(plusEquityEntity.getEquityId());
        if (integer > 0){
            return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
        }
        return ResponseResult.fail(BizCodeEnume.OPERATION_FAILED);
    }

    @Override
    public ResponseResult updatePlusEquity(PlusEquityDTO dto) {
        String userInfoStr = RedisUtil.get(YFConfig.YF_PLATFORM_TOKEN_USER + dto.getToken());
        if (null == userInfoStr || userInfoStr.equals("")) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }
        if (dto.getEquityStatus() == 0) {
            ArrayList<Object> list = new ArrayList<>();
            List<String> selectthalitypes = this.baseMapper.selectthalitypes();
            for (String selectthalitype : selectthalitypes) {
                String[] split = selectthalitype.split(",");
                List<Integer> appIdList = Stream.of(split).map(Integer::parseInt).collect(Collectors.toList());
                list.addAll(appIdList);
            }
            boolean contains = list.contains(dto.getEquityId());
            if (contains) {
                return ResponseResult.fail(BizCodeEnume.PLUS_ERROR2);
            }
        }
        Integer integer = this.baseMapper.update(null,new LambdaUpdateWrapper<UserPlusEquityEntity>()
                                                .set(dto.getEquityStatus()!=null,UserPlusEquityEntity::getEquityStatus,dto.getEquityStatus())
                                                .eq(UserPlusEquityEntity::getEquityId,dto.getEquityId()));
        if (integer > 0){
            return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
        }
        return ResponseResult.fail(BizCodeEnume.OPERATION_FAILED);
    }

    @Override
    public ResponseResult selectPlusEquity(Map<String, Object> map) {
        String userInfoStr = RedisUtil.get(YFConfig.YF_PLATFORM_TOKEN_USER + map.get("token").toString());
        if (null == userInfoStr || userInfoStr.equals("")) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }
        IPage<UserPlusEquityEntity> page=this.baseMapper.selectPlusEquityByPage(new Query<UserPlusEquityEntity>().getPage(map),map);
        if (page != null && page.getRecords().size() > 0){
            for (UserPlusEquityEntity plusEquityEntity : page.getRecords()) {
                //获取红包模板
                if (plusEquityEntity.getEquityType() == 2 || plusEquityEntity.getEquityType() == 3){
                    String equityValue = plusEquityEntity.getEquityValue();
                    String[] split = equityValue.split(",");
                    List<Integer> redIdList = Stream.of(split).map(Integer::parseInt).collect(Collectors.toList());
                    //查询红包模板
                    Map<String, Object> actMap = new HashMap<>();
                    actMap.put("id",redIdList);
                    List<RedTemplateEntityVO> result = activityServiceClient.selectredtemplateId(redIdList);
                    plusEquityEntity.setRedList(result);
                }
                if (plusEquityEntity.getEquityType() == 1){
                    BigDecimal multiply = new BigDecimal(plusEquityEntity.getEquityValue()).multiply(new BigDecimal(10));
                    plusEquityEntity.setEquityValue(multiply.toString());
                }
            }
        }
        return ResponseResult.success(PageUtils.getPage(page));
    }
}




